<?php
/*
	插件概览
*/
global $_RQ,$_AR;
$jigouid=$this->checklogin();
$_RQ['op']=$_RQ['op']?$_RQ['op']:'main';
if($_RQ['op']=='main'){
	$item=pdo_get('jy_chengedu_jigou',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
    $return['plugin_icon']=$item['logo'];
	$item['codeimg']=$item['codeimg']?json_decode($item['codeimg'],true):array();
	$item['pcd']=$item['pcd']?json_decode($item['pcd'],true):array('province'=>'');
	$item['onlinetime']=json_decode($item['onlinetime'],true);
	$return['status']=array('1'=>'开启','0'=>'关闭');
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'机构设置',
		'edit_tab'=>true,
		'edits'=>array(
				array(
					'title'=>'基本信息',
					'list'=>array(
							array('type'=>'string','must'=>true,'title'=>'机构名称','name'=>'title'),
							array('type'=>'image','title'=>'机构logo','name'=>'logo'),
							array('type'=>'text','title'=>'机构简介','name'=>'desc'),
							array('type'=>'string','title'=>'机构联系人','name'=>'realname'),
							array('type'=>'string','title'=>'机构手机','name'=>'mobile'),
							array('type'=>'pcd','title'=>'机构区域','name'=>'pcd'),
							array('type'=>'string','title'=>'机构地址','name'=>'address'),
							array('name'=>'codeimg','title'=>'营业执照/许可证','type'=>'images'),
							array('name'=>'content','title'=>'机构详情','type'=>'richtext'),	
					)
				),
			)
	);
	if($_SESSION['os']=='windows'){
		$return['htmls']=$htmls;
	}
	$return['account']=$_SESSION['account'];
	$return['item']=$item;
	$return['extradata']=array();
	$return['cate']=pdo_getall('jy_chengedu_category',array('uniacid'=>$_SESSION['uniacid']),array('id','catename'));
	if($_SESSION['os']=='mobile'){
		$return['htmls']=$this->template_url('manage/shop_index');
	}
	$return['province']=cfc('tools')->pcd();
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['pcd']['city']){
		exi('请选择机构城市','error');
	}
	if(!$postdata['title']){
		exi('机构名称必填','error');
	}
	if(!$postdata['lat_lng']){
		exi('请填写坐标！','error');
	}
	$jigouid=pdo_getcolumn('jy_chengedu_jigou',array('uniacid'=>$_SESSION['uniacid'],'id'=>$jigouid),'id');
	if($postdata){
		//页面设计
		if($postdata['items']){
			foreach($postdata['items'] as &$v){
				if($v['extradata']){
					unset($v['extradata']);
				}
			}
		}
		$diypage=array(
			'items'=>$postdata['items']?ijson_encode($postdata['items']):'',
			'setting'=>$postdata['setting']?ijson_encode($postdata['setting']):''
		);
		if($diypage['items'] || $diypage['setting']){
			//检测恶意代码
			if(strexists($diypage['items'],'<?') || strexists($diypage['setting'],'<?')){
				exi('提交内容存在恶意代码，无法提交！','error');
			}
			//获取开发者自己的组件，并上传
			$local_blocks=pdo_getall('core_mobile_blocks',array('madehtml <>'=>''),array('cloud_id','default','madehtml'),'cloud_id');
			if($local_blocks){
					foreach($local_blocks as &$row){
						$row['madehtml']=cfc('files')->source_read($row['madehtml']);
						$row['default']=$row['default']?json_decode($row['default'],true):array();
					}
			}
			//设计完成后，对接云端生成页面
			$post=array('items'=>base64_encode($diypage['items']),'setting'=>base64_encode($diypage['setting']),'local_blocks'=>$local_blocks);
			$html=cloud_request('diy.mobile.made',$post);
			//将获得生成的html代码存入文件
			$schooluser=pdo_get('jy_chengedu_jigou',array('id'=>$jigouid,'uniacid'=>$_SESSION['uniacid']),array('html'));
			$html=cfc('files')->source_save($html,'page','html','机构页面：'.$jigouid,$schooluser['html']);
			if(!$schooluser['html']){
					$schooluser['html']=$html;
			}
		}
		$data =array(
			'title'=>trim($postdata['title']),
			'topimg'=>trim($postdata['topimg']),
			'desc'=>$postdata['desc'],
			'remark'=>$postdata['remark'],
			'logo'=>$postdata['logo'],
			'realname'=>trim($postdata['realname']),
			'mobile'=>trim($postdata['mobile']),
			'offline'=>intval($post['offline']),
			'onlinetime'=>ijson_encode($postdata['onlinetime']),
			'lat_lng'=>ijson_encode($postdata['lat_lng']),
			'address'=>trim($postdata['address']),

			'cateid'=>intval($postdata['cateid']),

			'isrecommand'=>$postdata['isrecommand'],
			
			'groupid'=>$postdata['groupid'],
			'payopenid'=>$postdata['payopenid'],
			'isbrand'=>$postdata['isbrand'],
			'content'=>trim($postdata['content']),
			
			'codeimg'=>ijson_encode($postdata['codeimg']),
			'pcd'=>ijson_encode($postdata['pcd']),

			'items'=>$diypage['items'],
			'setting'=>$diypage['setting'],
			'html'=>$schooluser['html'],
		);
		if($jigouid){
			pdo_update('jy_chengedu_jigou',$data,array('uniacid'=>$_SESSION['uniacid'],'id'=>$jigouid));
		}else{
			$data['uniacid']=$_SESSION['uniacid'];
			$data['mid']=$_SESSION['uid'];
			$data['applytime']=time();
			pdo_insert('jy_chengedu_jigou',$data);
		}
		//添加客服
		$install = check_account_permission('jy_kefu');
		if($install){
			$member=pdo_get('core_members',array('id'=>$_SESSION['uid'],'uniacid'=>$_SESSION['uniacid']),array('nickname','avatar','mobile','groupid'));
			$kefuinfo=array(
				'nickname'=>trim($member['nickname']),
				'number'=>$postdata['mobile']?trim($postdata['mobile']):trim($member['mobile']),
				'picurl'=>trim($member['avatar']),
				'plugin'=>trim($member['plugin']),
				'pid'=>intval($jigouid),
				'mid'=>$_SESSION['uid'],
			);
			$kefuinfo['type']=0;
			$kefuinfo['name']='kefu'.$_SESSION['uid'];
			model('jy_kefu/kefu')->add($kefuinfo);
			$kefuinfo['type']=1;
			$kefuinfo['name']='kefu1_'.$_SESSION['uid'];
			model('jy_kefu/kefu')->add($kefuinfo);
			$kefuinfo['type']=2;
			$kefuinfo['name']='kefu2_'.$_SESSION['uid'];
			model('jy_kefu/kefu')->add($kefuinfo);
		}else{
			//exi('无法开启客服，联系管理员开启<全网客服>','error');
		}
	}
	exi('设置成功','','goback');
}
if($_RQ['op']=='diy'){
	$item=pdo_get('jy_chengedu_jigou',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
    $return['plugin_icon']=$item['logo'];
	$item['lat_lng']=$item['lat_lng']?json_decode($item['lat_lng'],true):array();
	$item['codeimg']=$item['codeimg']?json_decode($item['codeimg'],true):array();
	$item['onlinetime']=json_decode($item['onlinetime'],true);
	$item['myshop_tpl']=$item['myshop_tpl']?:0;
	$return['status']=array('1'=>'开启','0'=>'关闭');
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'DIY装修机构',
		'edit_tab'=>true,
		'edits'=>array(
				array(
					'title'=>'DIY装修机构',
					'list'=>array(
						array('type'=>'tip','title'=>'【温馨提示】：启用后DIY才生效！<red>有些组件《调用的》DIY时数据会不显示，但是真实预览会显示正常！</red>'),
						array('type'=>'switch','title'=>'启用DIY？','name'=>'opendiy','help'=>'开启后,DIY才生效！'),
						array('type'=>'diypage','title'=>'机构装修','name'=>'diypage'),
					)
				),
			)
	);
	$return['htmls']=$htmls;
	//页面设计
	$return['used_navs']=array();
	$demoitems=array(
		array('nid'=>76,'params'=>array()),
		array('nid'=>77,'params'=>array()),
		array('nid'=>78,'params'=>array()),
	);
	$item['items']=$item['items'] && $item['items']!='null'?json_decode($item['items'],true):$demoitems;
	if($item['items']){
			foreach($item['items'] as $row){
				if(!$return['used_navs'][$row['nid']]){
					$return['used_navs'][$row['nid']]=pdo_get('core_mobile_blocks',array('cloud_id'=>$row['nid']),array('name','showhtml','edithtml'));
				}
			}
	}
	$item['setting']=$item['setting'] && $item['setting']!='null'?json_decode($item['setting'],true):array('guide'=>array('title1'=>''));
	$navs0=pdo_getall('core_mobile_blocks',array('type'=>0,'basictype'=>0,'support <>'=>2),array(),'','usenum desc',array('10'));
	if($navs0){
			foreach($navs0 as &$nav){
				$nav['default']=json_decode($nav['default'],true);
			}
	}
	$navs1=pdo_getall('core_mobile_blocks',array('type'=>0,'basictype'=>1,'support <>'=>2),array(),'','usenum desc',array('10'));
	if($navs1){
			foreach($navs1 as &$nav){
				$nav['default']=json_decode($nav['default'],true);
			}
	}	
	// 应用组件
	$pluginparams = array('type'=>1,'support <>'=>2);
	$plugins = plugin('jy_diypage')->plugins('jy_chengedu');
	if(!empty($plugins)){
			$pluginparams['plugin_id']=$plugins;
			$navs2=pdo_getall('core_mobile_blocks',$pluginparams,array(),'','usenum desc',array('10'));
			if($navs2){
				$plugintitles=array();
				$plugins=array();
				$i = 0;
				foreach($navs2 as $k=>&$nav){
					$plugintitle=pdo_getcolumn('core_plugins',array('name'=>$nav['plugin_id']),'title');
					$nav['default']=json_decode($nav['default'],true);
				if (!$plugintitle || in_array($plugintitle,$plugintitles)){
						
					}else{
						$plugintitles[$i] = $plugintitle;
						$plugins[$i]['name'] = $nav['plugin_id'];
						$plugins[$i]['plugintitle']=$plugintitle;
						$blocks = pdo_getall('core_mobile_blocks',array('type'=>1,'plugin_id'=>$nav['plugin_id']),array(),'','usenum desc',array('10'));
						foreach($blocks as &$b){
							$b['default']=json_decode($b['default'],true);
						}
						$plugins[$i]['navs'] = $blocks;
						$i  += 1;
					}
					
				}
			}
	}
	$return['navs0']=$navs0;
	$return['navs1']=$navs1;
	$return['navs2']=$plugins;
	$return['edit']=3;
	$item['setting']['bg']=!empty($item['setting']['bg'])?$item['setting']['bg']:array('bg'=>'');
	$item['setting']['top']['title']=$item['setting']['top']['title']?$item['setting']['top']['title']:$item['title'];
	$return['menus']=pdo_getcolumn('core_diy_menu',array('uniacid'=>$_SESSION['uniacid'],'plugin'=>'core','status'=>1),'data');
	$return['menus']=json_decode($return['menus'],true);
	$item['setting']['footer']['menus']=!empty($item['setting']['footer']['menus']) ? $item['setting']['footer']['menus']:$return['menus']['menus'];
	$item['setting']['footer']['chosecolor']=$item['setting']['footer']['chosecolor']?:$_SESSION['customsyscolor'];
	$return['account']=$_SESSION['account'];
	$return['item']=$item;
	$return['extradata']=array();
	$return['cate']=pdo_getall('jy_chengedu_category',array('uniacid'=>$_SESSION['uniacid']),array('id','catename'));
	$return['myshop_tpls']=array(
		array('id'=>0,'title'=>'默认风格','image'=>'../addons/jy_chengedu/resource/images/tpl00.png'),
		array('id'=>1,'title'=>'外卖风格','image'=>'../addons/jy_chengedu/resource/images/tpl01.png'),
	);
	if($_SESSION['os']=='mobile'){
		$return['htmls']=$this->template_url('manage/shop_index');
	}
	exi($return);
}
if($_RQ['op']=='openvoice'){
	$post=get_postdata();
	$isopenvoice=intval($_RQ['isopenvoice']);
	pdo_update('jy_chengedu_jigou',array('isopenvoice'=>$isopenvoice),array('uniacid'=>$_SESSION['uniacid'],'id'=>$jigouid));
	exi('设置成功！');	
}